<?php
  namespace app\admin\controller;

    use app\admin\model\ApiSupplierModel;
    use app\admin\model\ApiUserModel;
    use app\admin\model\UserModel;

    /**
     * Func 操作类
     *
     * Class ApiSupplierController
     *
     * @package app\admin\controller
     */
    class ApiSupplierController extends BaseController
    {
        /**
         * Func 列表
         */
        public function index()
        {
            // 时间
            if ($this->start_time && $this->end_time)
            {
                $condition['a.create_time'] = ['between', [
                    strtotime($this->start_time), strtotime($this->end_time),
                ]];
            }

            // 关键词
            if ($this->keyword)
            {
                if ($this->searchType)
                {
                    $condition[$this->searchType] = ['like', '%' . $this->keyword . '%'];
                } else {
                    $condition['a.supplier_title|b.user_login'] = ['like', '%' . $this->keyword . '%'];
                }
            }

            $condition['a.id'] = ['egt', 0];

            // 字段
            $fieldArr = ['a.*', 'b.user_login'];

            // 连接表
            $joinArr = [['user b', 'a.user_id = b.id', 'left']];

            $infos['list'] = (new ApiSupplierModel())->getApiSupplierListInfo(
                $condition, ['a.id' => 'desc'], ['page' => $this->page, 'limit' => $this->limit],
                $fieldArr, $joinArr
            );

            $infos['supplierStatusArr'] = self::$supplierStatusArr;

            $this->assign('infos', $infos);

            return $this->fetch();
        }

        /**
         * Func 添加
         */
        public function add()
        {
            $param = $this->request->param();

            $usobj = new ApiUserModel();

            if ($this->request->isPost())
            {
                $result = $this->validate($param, 'ApiSupplier.add');
                if (!is_bool($result))
                {
                    $this->error($result);
                }

                // 账号
                $getUserOneInfo = $usobj->getApiUserOneInfo(['a.id' => (Int)$param['user_id']], ['a.id' => 'desc']);
                if (empty($getUserOneInfo))
                {
                    $this->error('账号不存在');
                }

                // 添加和更新的字段信息
                $data1 = self::getPostParamInfo([
                     'supplier_title', 'supplier_tel', 'supplier_desc'
                ]);

                // 附加参数
                $data2['user_id'] = $getUserOneInfo['id'];
                $data2['status'] = session('admin_is_operate') == 1 ? 1 : 3; // 非平台添加需要审核

                // 添加的数据
                $data = array_merge((array)$data1, (array)$data2);

                // 实例化模型类
                $sobj = new ApiSupplierModel();

                // 查询数据是否存在
                $condition_CH['a.user_id']        = ['eq', trim($data['user_id'])];
                $condition_CH['a.supplier_title'] = ['eq', trim($data['supplier_title'])];
                $getApiSupplierCount = $sobj->getApiSupplierCount($condition_CH);
                if (!empty($getApiSupplierCount))
                {
                    $this->error('数据已存在');
                }

                if ($sobj->addOrUpdateApiSupplierInfo($data))
                {
                    $this->success('添加成功!', url('index', []));

                } else {
                    $this->error('添加失败,请稍后重试');

                }
            }

            // 分销商账号
            $conditionUO['a.user_status'] = array('eq', 1);
            $conditionUO['a.id']          = array('in', $this->getUserIdArr([4]));
            $detail['getUserListInfo']    = $usobj->getApiUserListInfo(
                $conditionUO, ['a.id' => 'desc'], ['page' => 1, 'limit' => 100], ['a.id', 'a.user_nickname']
            );

            $detail['supplierStatusArr'] = self::$supplierStatusArr;

            $this->assign('detail', $detail);

            return $this->fetch();
        }

        /**
         * Func 修改
         */
        public function edit()
        {
            $param = $this->request->param();

            $usobj = new ApiUserModel();
            $spobj = new ApiSupplierModel();

            $detail = $spobj->getApiSupplierOneInfo(['uuid' => $param['uuid']], ['id' => 'desc']);
            if (!$detail)
            {
                $this->error('信息不存在');
            }

            if ($this->request->isPost())
            {
                $result = $this->validate($param, 'ApiSupplier.edit');
                if (!is_bool($result))
                {
                    $this->error($result);
                }

                // 账号
                $getUserOneInfo = $usobj->getApiUserOneInfo(['a.id' => (Int)$param['user_id']], ['a.id' => 'desc']);
                if (empty($getUserOneInfo))
                {
                    $this->error('账号不存在');
                }

                // 添加和更新的字段信息
                $data1 = self::getPostParamInfo([
                    'supplier_title', 'supplier_tel', 'supplier_desc'
                ]);

                // 附加参数
                $data2['user_id'] = $getUserOneInfo['id'];

                // 管理員操作状态
                if(session('admin_is_operate') == 1 && $detail['status'] != $param['status'] && in_array($param['status'],[-1,2,3]))
                {
                    $data2['status'] = $param['status'];
                }

                $data = array_merge((array)$data1, (array)$data2);
                if ($spobj->addOrUpdateApiSupplierInfo($data, $detail['id']))
                {
                    $this->success('修改成功!', url('index', []));
                } else {
                    $this->error('修改失败,请稍后重试');
                }
            }

            // 分销商账号
            $conditionUO['a.user_status'] = array('eq', 1);
            $conditionUO['a.id']          = array('in', $this->getUserIdArr([4]));
            $detail['getUserListInfo']    = $usobj->getApiUserListInfo(
                $conditionUO, ['a.id' => 'desc'], ['page' => 1, 'limit' => 100], ['a.id', 'a.user_nickname']
            );

            $detail['supplierStatusArr'] = self::$supplierStatusArr;

            $this->assign('detail', (array)$detail);

            return $this->fetch();
        }
    }